from django.shortcuts import render

# Create your views here.

from .models import New,Comment,User
from .ser import NewSweializer
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.paginator import Paginator

class Page(APIView):
    def get(self,request):
        data = request.query_params.get('page')
        new = New.objects.all()
        page = Paginator(new,2)
        page_list = [i for i in page.page_range]
        page_page = page.page(data)
        page_num = page.num_pages
        new_ser = NewSweializer(page_page,many=True)
        return Response({'page_list':page_list,'new_ser':new_ser.data})
class Login(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        User.objects.get(name=name,password=password)
        return Response({'msg':'登录成功','code':200})
class CommentView(APIView):
    def post(self,request):
        id = request.data.get('id')
        content = request.data.get('content')
        com = Comment.objects.get(id=id)
        com.content = content
        com.save()
        new = New.objects.get(id=id)
        new.new_comment += 1
        new.save()
        return Response({'msg':'评论已完成，评论次数已加一','code':200})

class NewView(APIView):
    def get(self,request):
        id = request.query_params.get('id')
        new = New.objects.get(id=id)
        new.browse += 1
        new.save()
        new_ser = NewSweializer(new)
        return Response({'new_ser':new_ser.data})


